//
//  BaseViewController.m
//  TenMinDemo
//
//  Created by Shawn Li on 2021/3/8.
//  Copyright © 2021 CYXiang. All rights reserved.
//

#import "BaseViewController.h"
#import "LogOnViewController.h"
#import "CYXNavigationController.h"

@interface BaseViewController ()<UIScrollViewDelegate>
/// 含有编辑的Scroll
@property (nonatomic, weak) UIScrollView *editScrollView;
/// editScroll 的下部约束
@property (nonatomic, weak) NSLayoutConstraint *editScrollBottom;
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.isHideTitle = YES;
    [self setupUI];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.isHideTitle) {
        self.navigationItem.title = @"";
    }else{
        self.navigationItem.title = self.title;
    }
    
        if (!self.isIgnoreLogon) {
            [LCNotifiCenter addObserver:(self) selector:(@selector(goToLogOn)) name:(@"UserNeedLogOn") object:nil];
        }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [LCNotifiCenter removeObserver:self name:@"UserNeedLogOn" object:nil];
}

- (void)tableView:(UITableView *)tabV refreshAction:(SEL)refreshSel loadMoreAction:(SEL)loadMoreSel{
    if (refreshSel) {
        tabV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:refreshSel];
    }
    if(loadMoreSel){
//        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreAction)];
        tabV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:loadMoreSel];
    }
}

- (void)setupUI{
    self.view.backgroundColor = MainColor;
}

- (void)leftNavItemClick:(id)sender{
    
}

- (void)rightNavItemClick:(id)sender{
    
}

- (void)setMainEditScrollScrollView:(UIScrollView *)scrollView andScrollViewBottom:(nonnull NSLayoutConstraint *)scrollBottom{
    self.editScrollView = scrollView;
    self.editScrollView.delegate = self;
    self.editScrollBottom = scrollBottom;
}

- (void)setRightNavItem:(NSString *)title font:(UIFont *)font color:(UIColor *)color icon:(NSString *)icon{
    
    self.navigationItem.rightBarButtonItem = [self getBarItem:title font:font color:color icon:icon sel:@selector(rightNavItemClick:) isLeft:NO];
}

- (void)setLeftNavItem:(NSString *)title font:(UIFont *)font color:(UIColor *)color icon:(NSString *)icon{
    
    self.navigationItem.leftBarButtonItem = [self getBarItem:title font:font color:color icon:icon sel:@selector(leftNavItemClick:) isLeft:YES];
}

- (UIBarButtonItem *)getBarItem:(NSString *)title font:(UIFont *)font color:(UIColor *)color icon:(NSString *)icon sel:(SEL)sel isLeft:(BOOL)isLeft{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0,0,80,30)];
    [btn addTarget:self action:sel forControlEvents:(UIControlEventTouchUpInside)];
    if (title) {
        [btn setTitle:title forState:(UIControlStateNormal)];
    }
    if (icon) {
        [btn setImage:[UIImage imageNamed:icon] forState:(UIControlStateNormal)];
    }
    if (font){
        btn.titleLabel.font = font;
    }
    if (color){
        btn.titleLabel.textColor = color;
    }
    [btn setContentHorizontalAlignment:(isLeft?UIControlContentHorizontalAlignmentLeft:UIControlContentHorizontalAlignmentRight)];
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    [self.view endEditing:YES];
}

-(void)keyboardChangeFrame:(NSNotification*)noti;
{
    
    CGRect rect= [noti.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
    NSLog(@"%@", noti.userInfo);
    CGFloat keyboardheight = rect.size.height;
    CGFloat duration=[noti.userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
    CGFloat curve=[noti.userInfo[UIKeyboardAnimationCurveUserInfoKey]doubleValue];

    [UIView setAnimationCurve:curve];
    [UIView animateWithDuration:duration animations:^{
        
        if (self.editScrollView && self.editScrollBottom) {
            self.editScrollBottom.constant = keyboardheight;
            [self loadViewIfNeeded];
        }
        
      
    } completion:^(BOOL finished) {
        
    }];
    
}


-(void)keyboardHide:(NSNotification *)noti
{
//    [self.view endEditing:YES];
//
    [UIView animateWithDuration:0.3 animations:^{
        if (self.editScrollView && self.editScrollBottom) {
            self.editScrollBottom.constant = 0;
            [self loadViewIfNeeded];
        }
      
    } completion:^(BOOL finished) {
        
    }];

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    [self.view endEditing:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

- (void)popVCTo:(NSString *)vcClassName{
    Class clazz = NSClassFromString(vcClassName);
    UIViewController *keyVc = nil;
    
    for (NSInteger i = self.navigationController.viewControllers.count - 1; i>=0; i--) {
        UIViewController *vc
         = self.navigationController.viewControllers[i];
        if ([vc isKindOfClass:clazz]) {
            keyVc = vc;
            break;
        }
    }
    if (keyVc) {
        [self.navigationController popToViewController:keyVc animated:YES];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (void)goToLogOn{
    LogOnViewController *vc = LoadVCFormXib(LogOnViewController);
    CYXNavigationController *navC = [[CYXNavigationController alloc]initWithRootViewController:vc];
    [navC setBarStyle:YES];
    //设置模态视图弹出样式
    navC.modalPresentationStyle = UIModalPresentationOverFullScreen;

    [kAppDelegate.getCurrentVC presentViewController:navC animated:YES completion:nil];
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




@end
